package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.RoleFunctionControllerSwagger;
import com.youan.serve.dao.domain.RoleFunction;
import com.youan.serve.service.IRoleFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/role-function")
public class RoleFunctionController implements RoleFunctionControllerSwagger{
    @Autowired
    private IRoleFunctionService roleFunctionService;

    @Override
    @PostMapping("/roleFunction")
    public RoleFunction addRoleFunction(RoleFunction roleFunction, LoginInfor loginInfor){
        roleFunction.setRoleFunctionId(UUID.randomUUID().toString().replaceAll("-", ""));
        roleFunction.setOperator(loginInfor.getUsername());
        roleFunction.setOperateTime(new Date());
        roleFunctionService.save(roleFunction);
        return null;
    }

    @Override
    @PutMapping("/roleFunction")
    public RoleFunction updateRoleFunction(RoleFunction roleFunction, LoginInfor loginInfor){
        roleFunction.setOperator(loginInfor.getUsername());
        roleFunction.setOperateTime(new Date());
        roleFunctionService.updateById(roleFunction);
        return null;
    }

    @Override
    @GetMapping("/roleFunctions")
    public List<RoleFunction> selectAllRoleFunction(){
        return roleFunctionService.list();
    }

    @Override
    @DeleteMapping("/roleFunction/{roleFunctionId}")
    public void deleteRoleFunctionByRoleFunctionId(@PathVariable String roleFunctionId){
        roleFunctionService.removeById(roleFunctionId);
    }

    @Override
    @GetMapping("/roleFunction/{roleFunctionId}")
    public RoleFunction selectRoleFunctionByRoleFunctionId(@PathVariable String roleFunctionId){
        return roleFunctionService.getById(roleFunctionId);
    }

    @Override
    @GetMapping("/roleFunction/roleId/{roleId}")
    public List<String> selectRoleFunctionByRoleId(@PathVariable String roleId) throws Exception{
        return roleFunctionService.getSelectedFunctions(roleId);
    }

    @Override
    @DeleteMapping("/roleFunction/function/{functionId}/roleId/{roleId}")
    public void deleteRoleFunctionByFunctionIdAndRoleId(@PathVariable String roleId, @PathVariable String functionId){
        roleFunctionService.remove(new LambdaUpdateWrapper<RoleFunction>().eq(RoleFunction::getFunctionId, functionId).eq(RoleFunction::getRoleId, roleId));
    }
}

